POJ 1236 Network of Schools (有向图的强连通分量)

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9073   Accepted: 3594

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school. 

Input

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Source

 
 
 
这题给了一个有向图。
 
需要解决两个问题:
第一是需要给多少个点,才能传遍所有点。
 
第二问是加多少条边,使得整个图变得强连通。
 
使用Tarjan进行缩点,得到一个SCC图、
 
这个图有多少个入度为0的,多少个出度为0的。
 
假设有n个入度为0,m个出度为0
 
那么第一个答案就是n,第二个答案是max(n,m)
 
 
具体证明不解释了,貌似以前做过的题目,有解释。
 
需要注意的是假如只有一个强连通分量,即整个图是连通的,那么第一个答案是1,第二个答案是0
 
 
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 110;
const int MAXM = 110*110;

struct Edge
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[MAXN];

void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}
void Tarjan(int u)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if(!DFN[v])
        {
            Tarjan(v);
            if(Low[u] > Low[v])
                  Low[u] = Low[v];
        }
        else if(Instack[v] && Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Belong[v] = scc;
            Instack[v] = false;
        }
        while( v!= u);
    }
}
int in[MAXN],out[MAXN];
void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    Index = scc = top = 0;
    for(int i = 1;i <= N;i++)
        if(!DFN[i])
           Tarjan(i);
    if(scc == 1)
    {
        printf("1\n0\n");
        return;
    }
    for(int i = 1;i <= scc;i++)
       in[i] = out[i] = 0;
    for(int u = 1;u <= N;u++)
    {
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            int v = edge[i].to;
            if(Belong[u] != Belong[v])
            {
                in[Belong[v]]++;
                out[Belong[u]]++;
            }
        }
    }
    int ans1=0,ans2=0;
    for(int i = 1;i <= scc;i++)
    {
        if(in[i]==0)ans1++;
        if(out[i]==0)ans2++;
    }
    printf("%d\n%d\n",ans1,max(ans1,ans2));

}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
int main()
{
    int n;
    int v;
    while(scanf("%d",&n) == 1)
    {
        init();
        for(int i = 1;i <= n;i++)
        {
            while(scanf("%d",&v)==1 && v)
            {
                addedge(i,v);
            }
        }
        solve(n);
    }
    return 0;
}

 

 
 
 
 
 
 
 
 

posted on 2013-07-10 18:07  kuangbin  阅读(1808)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: